<%@ page import="com.example.demo_text.CourseDAO" %>
<%@ page import="com.example.demo_text.Assignment" %>
<%@ page import="com.example.demo_text.AssignmentDAO" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String courseId = request.getParameter("course_id");
    
    // 调试输出
    System.out.println("assignments.jsp - 接收到的courseId: " + courseId);
    
    String courseName = "";
    if (courseId != null && !courseId.isEmpty()) {
        courseName = CourseDAO.getCourseNameById(courseId);
    }
    
    // 获取该课程的所有作业
    List<Assignment> assignments = null;
    if (courseId != null && !courseId.isEmpty()) {
        assignments = AssignmentDAO.getAssignmentsByCourseId(courseId);
    }
%>
<html>
<head>
    <title>作业管理</title>
    <style>
        /* 如上一致样式 */
        * { box-sizing: border-box; margin: 0; padding: 0; }
        body {
            font-family: 'Segoe UI', sans-serif;
            background: linear-gradient(135deg, #e0f7fa, #f3e5f5);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .card {
            background: white;
            width: 90%;
            max-width: 800px;
            padding: 30px 25px;
            border-radius: 16px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
        }
        h2 {
            text-align: center;
            margin-bottom: 20px;
            color: #374151;
        }
        p {
            text-align: center;
            color: #4b5563;
            margin-bottom: 20px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }
        th, td {
            padding: 10px;
            border-bottom: 1px solid #ddd;
            text-align: center;
        }
        a.btn-back {
            display: inline-block;
            padding: 10px 20px;
            background-color: #f3f4f6;
            color: #374151;
            border-radius: 8px;
            text-decoration: none;
            font-weight: 500;
            transition: all 0.3s ease;
        }
        a.btn-back:hover {
            background-color: #d1d5db;
            color: #111827;
        }
        
        /* Form styles */
        .assignment-form {
            margin: 20px 0;
            padding: 20px;
            background-color: #f9fafb;
            border-radius: 8px;
        }
        .assignment-form h3 {
            margin-bottom: 15px;
            color: #374151;
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: 500;
        }
        .form-group input, .form-group textarea {
            width: 100%;
            padding: 10px;
            border: 1px solid #d1d5db;
            border-radius: 6px;
        }
        .form-group textarea {
            height: 100px;
            resize: vertical;
        }
        button {
            background-color: #3b82f6;
            color: white;
            border: none;
            padding: 10px 20px;
            font-size: 16px;
            border-radius: 6px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }
        button:hover {
            background-color: #2563eb;
        }
        
        /* Status colors */
        .status-active {
            color: green;
        }
        .status-pending {
            color: blue;
        }
    </style>
</head>
<body>

<div class="card">
    <h2>作业管理</h2>
    <% if (!courseName.isEmpty()) { %>
    <p>当前课程名称：<%= courseName %></p>
    <% } else { %>
    <p>未找到课程信息或未指定课程 ID。</p>
    <% } %>

    <!-- 发布作业表单 -->
    <div class="assignment-form">
        <h3>发布新作业</h3>
        <form action="PublishAssignmentServlet" method="post">
            <input type="hidden" name="course_id" value="<%= courseId %>"/>

            <div class="form-group">
                <label for="assignment_title">作业标题：</label>
                <input type="text" id="assignment_title" name="assignment_title" required/>
            </div>

            <div class="form-group">
                <label for="assignment_content">作业内容：</label>
                <textarea id="assignment_content" name="assignment_content" required></textarea>
            </div>

            <div class="form-group">
                <label for="due_date">截止日期：</label>
                <input type="date" id="due_date" name="due_date" required/>
            </div>

            <div class="form-group">
                <label for="max_score">满分值：</label>
                <input type="number" id="max_score" name="max_score" min="1" required/>
            </div>

            <button type="submit">发布作业</button>
        </form>
    </div>

    <!-- 作业列表 -->
    <div class="module-content">
        <h3>作业列表</h3>
        <table>
            <tr>
                <th>作业标题</th>
                <th>内容摘要</th>
                <th>截止日期</th>
                <th>满分值</th>
                <th>状态</th>
            </tr>
            <% if (assignments != null && !assignments.isEmpty()) { %>
                <% for (Assignment assignment : assignments) { %>
                <tr>
                    <td><%= assignment.getAssignmentTitle() %></td>
                    <td><%= assignment.getAssignmentContent().length() > 30 ? 
                        assignment.getAssignmentContent().substring(0, 30) + "..." : 
                        assignment.getAssignmentContent() %></td>
                    <td><%= assignment.getDueDate() %></td>
                    <td><%= assignment.getMaxScore() %></td>
                    <td><span class="<%= assignment.isActive() ? "status-active" : "status-pending" %>">
                        <%= assignment.isActive() ? "进行中" : "已结束" %></span>
                    </td>
                </tr>
                <% } %>
            <% } else { %>
                <tr><td colspan="5">暂无作业安排</td></tr>
            <% } %>
        </table>
        <p>请按时提交你的作业。</p>
    </div>

</div>

</body>
</html>
